import java.util.ArrayList;


public class QuantitiesView extends View implements RectangularButtonDelegate{

	private ArrayList<Quantity> quantities;
	private int startIndex, endIndex;
	private RectangularButton nextQuantities, previousQuantities;
	public QuantitiesView(float x_, float y_, float w_, float h_, MainView p) {
		super(x_, y_, w_, h_, p);
		this.startIndex = 0;
		this.endIndex = 8;
		this.previousQuantities= new RectangularButton(250, h-50, 100, 30,parent,1);
		this.previousQuantities.setCaption("Previous");
		this.nextQuantities = new RectangularButton(360, h-50, 100, 30,parent,2);
		this.nextQuantities.setCaption("Next");
		//this.subviews.add(nextResults);
		this.nextQuantities.setDelegate(this);
		//this.subviews.add(previousResults);
		this.previousQuantities.setDelegate(this);
	}
	public void addQuantity(Quantity q){
		if(quantities == null)
			quantities = new ArrayList<Quantity>();
		this.quantities.add(q);
		System.out.println("EndIndex = "+ endIndex +" and Quantities.size = "+ this.quantities.size());
		
			if((endIndex+1)-startIndex <= 8)
				endIndex++;
		
		
	}
	public void setQuantities(ArrayList<Quantity> q){
		this.quantities=q;
	}
	public ArrayList<Quantity> getQuantities(){
		return this.quantities;
	}
	void drawContent()
	  {
	   parent.fill(255);
	   parent.stroke(110);
	   parent.strokeWeight(3);
	   parent.rect(0,0,w,h);
	   if(quantities== null){
		   parent.fill(110);
		   parent.textSize(20);
		   parent.textAlign(parent.LEFT, parent.BASELINE);
		   parent.text("No quantities exist for this product.", w/2-175, h/2);
	   }
	  }
	void updateSubViews(){
		int y = 0;
		this.subviews = new ArrayList<View>();
		for(int i = startIndex; i < endIndex && i < this.quantities.size(); i++){
			QuantityBox qtemp  = new QuantityBox(0,y,w,30,parent,this.quantities.get(i));
			this.subviews.add(qtemp);
			 y += 30;
		}System.out.println("End Index = "+endIndex +" Quantities "+ this.quantities.size());
		if(endIndex < this.quantities.size())
			if(!this.subviews.contains(this.nextQuantities))this.subviews.add(this.nextQuantities);
		if(startIndex > 0)
			if(!this.subviews.contains(this.previousQuantities))this.subviews.add(this.previousQuantities);
	}
	@Override
	public void RectangularButtonPressed(int id) {
		if(id == 1){
			//if(startIndex > 0){
				endIndex = startIndex;
				startIndex = startIndex - 8;
				if(startIndex <0)
					startIndex = 0;
			//}

				System.out.println("start = "+startIndex +" end = "+ endIndex);
			    this.updateSubViews();
				if(startIndex == 0){
				if(this.subviews.contains(previousQuantities)) this.subviews.remove(previousQuantities);
				
			}
			
		}
		if(id == 2){
			// Next
			startIndex = endIndex;
			endIndex = endIndex + 8;
			if(endIndex > this.quantities.size())
				endIndex = this.quantities.size();
			System.out.println("start = "+startIndex +" end = "+ endIndex);
		    this.updateSubViews();
			
			if(endIndex >= this.quantities.size()){
				if(this.subviews.contains(nextQuantities)) this.subviews.remove(nextQuantities);
				
			}
			
		}
		
	}

}
